package com.example.androidproject.service.config;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.example.androidproject.service.MusicService;
import com.hyg.common.utils.LogUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * FileName: MusicManager
 * Author: huangyuguang
 */
public class MusicManager {

    private static MusicManager instance;
    private List<OnSongChangeListener> onSongChangeListenerList = new LinkedList<>();
    private Context mContext;
    private MusicService musicService;

    private static final long HEART_BEAT_RATE = 10 * 1000;//每隔10s发送空消息保持WebSocket长连接

    public static MusicManager getInstance(){
        if(instance == null){
            synchronized (MusicManager.class){
                if(instance == null){
                    instance = new MusicManager();
                }
            }
        }
        return instance;
    }

    public void init(Context context){
        this.mContext = context;
        startService(mContext);
    }

    /**
     * 开启并绑定Timer服务
     */
    public void startService(Context context) {
        Intent bindIntent = new Intent(context, MusicService.class);
        context.startService(bindIntent);
        context.bindService(bindIntent, serviceConnection, context.BIND_AUTO_CREATE);
    }

    /**
     * 停止Timer服务
     */
    public void stopService(Context context) {
        try{
            Intent bindIntent = new Intent(context, MusicService.class);
            context.stopService(bindIntent);
            context.unbindService(serviceConnection);
        }catch (Exception e){}
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //服务与活动成功绑定
            musicService = ((MusicService.MusicClientBinder) iBinder).getService();
            LogUtils.e("Service", "服务与context成功绑定");
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            //服务与活动断开
            musicService = null;
            LogUtils.e("Service", "服务与context成功断开");
        }
    };

    public ArrayList<String> getMusicPaths() {
        if (musicService != null) {
            return musicService.getMusicPaths();
        }
        return null;
    }

    public void seekTo(int progress) {
        if (musicService != null && musicService.getMusicPaths().size() > 0) {
            musicService.getMediaPlayer().seekTo(progress * musicService.getMediaPlayer().getDuration() / 100);
        }
    }

    public void startOrPause(){
        if (musicService != null) {
            if(musicService.isStartPlaying()){
                musicService.pause();
            }else {
                musicService.start();
            }
        }
    }

//    public void pause(){
//        if (musicService != null) {
//            musicService.pause();
//        }
//    }

    public void playNext(){
        if (musicService != null){
            musicService.playNext();
        }
    }

    public void playBeforeMusic(){
        if(musicService != null){
            musicService.playBeforeMusic();
        }
    }

    public void playIndex(int musicIndex){
        if(musicService != null){
            musicService.playIndex(musicIndex);
        }
    }

    public String getMusicName(){
        String name = "";
        if(musicService != null) {
            name = musicService.getMusicName();
        }
        return name;
    }

    public boolean isPlaying(){
        if(musicService != null) {
            return musicService.isStartPlaying();
        }
        return false;
    }

    public void release(){
        stopService(mContext);
    }

    public List<OnSongChangeListener> getOnSongChangeListenerList() {
        return onSongChangeListenerList;
    }

    public void addOnSongChangeListener(OnSongChangeListener songChangeListener){
        onSongChangeListenerList.add(songChangeListener);
    }

    public void removeOnSongChangeListener(OnSongChangeListener songChangeListener){
        onSongChangeListenerList.remove(songChangeListener);
    }

    public interface OnSongChangeListener {
        void changeSong(int duration,String name,String artist);
        void onProgress(int duration,int currentPosition);
        void onStateChange(boolean isPlaying);
    }
}
